package com.pengtu.web.contract;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.entity.contract.According;
import com.pengtu.entity.contract.Document;
import com.pengtu.model.Acc;
import com.pengtu.service.contract.AccordingManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({@Result(name="itemList",location="${ctx}/contract/itemlist.action?cid=${cid}&decorator=${decorator}&confirm=${confirm}",type="redirect")})
public class AccordingAction extends BaseActionSupport<According> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private According according;
	private AccordingManager accordingManager;
	private String id;
	private String [] accorArray={};
	private List<According> accorlist=new ArrayList<According>();
	private String cid;
	private String projectId;
	private List<Acc> acc = new ArrayList<Acc>();
	private String decorator;
	private String confirm;

	/**
	 * decorator
	 *
	 * @return  the decorator
	 * @since   1.0.0
	 */
	
	public String getDecorator() {
		return decorator;
	}

	/**
	 * @param decorator the decorator to set
	 */
	public void setDecorator(String decorator) {
		this.decorator = decorator;
	}

	/**
	 * confirm
	 *
	 * @return  the confirm
	 * @since   1.0.0
	 */
	
	public String getConfirm() {
		return confirm;
	}

	/**
	 * @param confirm the confirm to set
	 */
	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	/**
	 * acc
	 *
	 * @return  the acc
	 * @since   1.0.0
	 */
	
	public List<Acc> getAcc() {
		return acc;
	}

	/**
	 * @param acc the acc to set
	 */
	public void setAcc(List<Acc> acc) {
		this.acc = acc;
	}

	public String getProjectId() {
		return projectId;
	}

	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public List<According> getAccorlist() {
		return accorlist;
	}

	public void setAccorlist(List<According> accorlist) {
		this.accorlist = accorlist;
	}

	public String[] getAccorArray() {
		return accorArray;
	}

	public void setAccorArray(String[] accorArray) {
		this.accorArray = accorArray;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public AccordingManager getAccordingManager() {
		return accordingManager;
	}

	@Autowired
	public void setAccordingManager(AccordingManager accordingManager) {
		this.accordingManager = accordingManager;
	}

	public According getAccording() {
		return according;
	}

	public void setAccording(According according) {
		this.according = according;
	}

	@Override
	public According getModel() {
		// TODO Auto-generated method stub
		return according;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub

		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		if(org.apache.commons.lang.StringUtils.isNotEmpty(cid)){
			Document document = accordingManager.findDocumentByValue("id", cid);
			projectId = document.getPid();
			List<According> accordingList = accordingManager.findAccordingList(document);
			Struts2Utils.getRequest().setAttribute("accordingList", accordingList);
		}
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		Document document = new Document();
		if(org.apache.commons.lang.StringUtils.isNotEmpty(cid)){
			accordingManager.deleteAccordCid(cid);
			document = accordingManager.findDocumentByValue("id", cid);
		}
		document.setSign("0");
		document.setPid(projectId);
		accordingManager.saveDocument(document);
		cid = document.getId();
		List<According> accordingList = new ArrayList<According>();
		for(int i = 1;i < acc.size();i++){
			Acc ac = acc.get(i);
			if(ac.getValue()!=null && !ac.getValue().equals("")){
				According according = new According();
				according.setIndex(Integer.parseInt(ac.getValue()));
				according.setName(ac.getLabel());
				according.setDocument(document);
				accordingList.add(according);
			}
		}
		accordingManager.saveAccordingList(accordingList);
		return "itemList";
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id)){
			according=accordingManager.findAccorByValue("id", id);
		}else{
			according=new According();
		}
	}

}
